package com.mrjy.share.bo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.ShareApplyInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.share.dao.IShareApplyDao;

@Service("shareApplyBo")
public class ShareApplyBo implements IShareApplyBo {
	
	@Resource
	private IUserInfoDao userDao;
	@Resource
	private IShareApplyDao shareApplyDao;
	
	/**
	 * 一级分销提交申请
	 */
	@Override
	public void txApply(UserInfo userInfo, Map<String , Object> map) {
		int flag=userDao.updateById(userInfo);
		if(flag > 0){
			ShareApplyInfo sa=new ShareApplyInfo();
			sa.setUserId(userInfo.getId());
			sa.setApplyTime(new Date());
			shareApplyDao.insert(sa);
			
			map.put("err_code", 0);
			map.put("err_msg", "申请成功");
		}else {
			map.put("err_code", -1);
			map.put("err_msg", "提交申请失败");
		}
				
	}
	
	@Override
	public ShareApplyInfo queryApplyStatus(ShareApplyInfo sa) {
		return shareApplyDao.queryApplyStatus(sa);
	}
	
	@Override
	public int txApplyStatus(ShareApplyInfo sa) {
		return shareApplyDao.updateById(sa);
	}
	

	@Override
	public List<ShareApplyInfo> listShareApplyInfo(ShareApplyInfo sa) {
		return shareApplyDao.listShareApplyInfo(sa);
	}
	/**
	 * 检查赚分享金用户为普通会员是否处于审核状态
	 * @param sa
	 */
	@Override
	public void checkUserAuditStatus(ShareApplyInfo sa, Map<String,Object> map) {
		ShareApplyInfo info = shareApplyDao.selectByUserId(sa);
		if (null == info) { //查询为空，则用户未处于审核状态
			map.put("err_code", -1);
			map.put("err_msg", "未发起审核");
		}else {
			info.setApplyTimeStr(DateUtil.dateToString(info.getApplyTime(), 2));
			if(info.getAuditStatus() == 0){//审核中
				map.put("err_code", 0);
				map.put("err_msg", "审核中");
				map.put("shareApplyInfo", info);
			}else if (info.getAuditStatus() == 1) { // 审核通过
				map.put("err_code", 1);
				map.put("err_msg", "未审核");
				map.put("shareApplyInfo", info);
			}else if (info.getAuditStatus() == 2) {//审核未通过
				map.put("err_code", 2);
				map.put("err_msg", "审核未通过");
				map.put("shareApplyInfo", info);
			}else{
				map.put("err_code", -2);
				map.put("err_msg", "审核状态字段取值不对，请检查");
			}
		}
	}
	/**
	 * 更新用户银行卡等信息
	 */
	@Override
	public void txUserInfo(UserInfo user, Map<String, Object> map) {
		int flag=userDao.updateById(user);
		if (flag > 0) {
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", -1);
			map.put("err_msg", "更新异常");
		}
	}
	/**
	 * 检查本月是否可以更新银行账号信息
	 */
	@Override
	public void isCanChange(UserInfo user, Map<String, Object> map) {
		UserInfo userInfo = userDao.queryById(user.getId());
		if (null == userInfo) {
			map.put("err_code", -1);
			map.put("err_msg", "查询异常");
		}else {
			if (null == userInfo.getModiferTime()) { //表示没有修改过，则可以修改
				map.put("err_code", 0);
				map.put("err_msg", "本月还剩最后一次更新机会");
			}else {
				//比较是不是本月已修改
				Boolean flag = DateUtil.isThisTime(userInfo.getModiferTime(), "yyyy-MM");
				if (flag) { //true ,代表末次修改时间ModiferTime为当月，则不能修改
					map.put("err_code", 1);
					map.put("err_msg", "本月已没有更新机会");
				}else{
					map.put("err_code", 0);
					map.put("err_msg", "本月还剩最后一次更新机会");
				}
			}
		}
		
	}
	
	/*@Override
	public List<ShareApplyInfo> listShareCommisionInfo(ShareApplyInfo sa) {
		return shareApplyDao.listShareCommisionInfo(sa);
	}
   */



	

}
